package com.huazhi.wzsnw.timer;

import com.huazhi.wzsnw.constant.DtuMapUtil;
import com.huazhi.wzsnw.exportData.ExportDataUtil;
import com.huazhi.wzsnw.mapper.*;
import com.huazhi.wzsnw.model.Alert;
import com.huazhi.wzsnw.model.Ddlined;
import com.huazhi.wzsnw.model.Lined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class TimerImp/* implements TimingListener */{

    private SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private AlertMapper alertMapper;
    @Autowired
    private LinedStateMapper linedStateMapper;
    @Autowired
    private LinedMapper linedMapper;
    @Autowired
    private AlertValueMapper alertValueMapper;
    @Autowired
    private DtuRecordMapper dtuRecordMapper;
    @Autowired
    private  ExportDataUtil exportDataUtil;
    @Autowired
    private  DdlinedMapper ddlinedMapper;
    @Autowired
    private  SxtlxGl sxtlxGl;


//    @Scheduled(cron="0 0 0/1 * * ? ")
//    public void MinuteTask() {
//        List<Lined> list = linedMapper.selectBuState("2");
//        AlterUtil alterUtil = new AlterUtil();
//        for (Lined lined :list){
//            if(null!=lined.getNumber()&&!"".equals(lined.getNumber())){
//                alterUtil.addAlter("DTU","DTU断开连接",lined,true);
//            }
//        }
//    }

    /*
     * 定时删除dtu数据表
     * 每五分钟执行一次
     * @author zhengch
     * @date 2021/7/20 2:46 下午
     */
    @Scheduled(cron="0 0/5 * * * ? ")
    public void delectRecord() {
     dtuRecordMapper.delectAll();
    }
    /*
     * 每隔一分钟 每隔dtu心跳是否正常
     * @author zhengch
     * @date 2021/7/16 1:41 下午
     * @param null
     * @return null
     */
    @Scheduled(cron="0 */2 * * * ?")
    public  void heart(){
        System.out.println("--------------------开始查看心跳数据------------------------------");
        AlterUtil alterUtil = new AlterUtil();
        Map<String,Date> map = DtuMapUtil.getInstance().getHeartMap();
        //遍历map集合 过滤dtu断开连接情况
        for (Map.Entry<String,Date> entry: map.entrySet()){
          Date date = entry.getValue();
            Lined lined =  linedMapper.getByDtu(entry.getKey());
          //如果已经超过一小时
          if(new Date().getTime() - date.getTime()>1000*60*60){
              alterUtil.addAlter("DTU","DTU断开连接",lined,true);
          }else {
              alterUtil.addAlter("DTU","DTU断开连接",lined,false);
          }
        }

    }

    /*
     * 每隔二分钟查看dtu是否发送数据
     * @author zhengch
     * @date 2021/7/16 1:41 下午
     * @param null
     * @return null
     */
    @Scheduled(cron="0 */2 * * * ?")
    public  void sendData(){
        System.out.println("--------------------开始查看dtu发送数据------------------------------");
    Map<String,Date> map = DtuMapUtil.getInstance().getSendMap();
        for (Map.Entry<String,Date> entry:map.entrySet()){
       Date date = entry.getValue();
       AlterUtil alterUtil = new AlterUtil();
       String number = entry.getKey();
       if(number.contains("DSG4RV1")){
           Ddlined ddlined = ddlinedMapper.selectByDtuNum(number);
           if(new Date().getTime()-date.getTime()>1000*60*120){
               alterUtil.addDcongdaAlter("DTU","DTU内部异常",ddlined,true);
           }else {
               alterUtil.addDcongdaAlter("DTU","DTU内部异常",ddlined,false);
               alterUtil.addDcongdaAlter("DTU","DTU断开连接",ddlined,false);
           }
       }else {
           Lined lined = linedMapper.getByDtu(number);
           //如果超过两个小时
           if(new Date().getTime()-date.getTime()>1000*60*120){
               alterUtil.addAlter("DTU","DTU内部异常",lined,true);
           }else {
               alterUtil.addAlter("DTU","DTU内部异常",lined,false);
               alterUtil.addAlter("DTU","DTU断开连接",lined,false);
           }
       }

    }
    }

    /*
     * 每隔二分钟 过滤报警表
     * @author zhengch
     * @date 2021/7/16 1:41 下午
     * @param null
     * @return null
     */
    @Scheduled(cron="0 */2 * * * ?")
    public  void checkLined(){
        System.out.println("--------------------开始过滤报警表------------------------------");
        List<String> numberList = DtuMapUtil.getInstance().getNumberList();
        Map<String,Integer> zidian = new HashMap<>();
        zidian.put("dklj",100);
        zidian.put("nbyc",10);
        zidian.put("bj",1);
        zidian.put("zc",0);
       for (String number:numberList){
           Integer flag = 0 ;
           String liendState ="0";
           List<Alert> list = alertMapper.selectAllBynumber(number);
           System.out.println("------------------"+number+"alter为:"+list.size()+"个-----------------------------");
         for (Alert alert:list){
         if(alert.getReason().endsWith("报警")&&zidian.get("bj")>flag){
             flag = zidian.get("bj");
             liendState = "1";
         }
         if("DTU内部异常".equals(alert.getReason())&&zidian.get("nbyc")>flag){
             flag = zidian.get("nbyc");
             liendState = "3";
         }
         if("DTU断开连接".equals(alert.getReason())&&zidian.get("dklj")>flag){
             flag = zidian.get("dklj");
             liendState ="2";
         }
         }
           System.out.println("------------------"+number+"state为:"+liendState+"-----------------------------");
         if(number.contains("DSG4RV1")){
             ddlinedMapper.updateStateByNumber(liendState,number);
         }else {
             linedMapper.updateStateByNumber(liendState,number);
         }

       }

    }


    /*
     * 每隔五分钟清除脏数据
     * @author zhengch
     * @date 2021/7/6 3:24 下午
     * @param null
     * @return null
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public  void cleanData(){
        System.out.println("定时清除脏数据");
        linedStateMapper.delAllDirtyData();
         alertValueMapper.delDirtyData();
        System.out.println("清除完毕！！！！！！！！！！！！！！！！！！");
    }

    /*
     *  每天23点清除报警表已处理数据
     * @author zhengch
     * @date 2021/11/24 4:31 下午
     * @param null
     * @return null
     */

//    @Scheduled(cron = "0 0 23 * * ? *")
    public void  cleanAlter(){
        System.out.println("清除已处理报警数据");
        alertMapper.delectAllYcl();

    }

    //每天凌晨一点开始检测更新萤石云摄像头在线状态
    @Scheduled(cron="0 0 01 * * ?")
    public void  updateViode(){
        sxtlxGl.startUpdateVideo();
    }

    //根据摄像头编号查询缺失的摄像头
   // @Scheduled(cron="0 21 11 * * ?")
    public  void checkVideo(){
       sxtlxGl.sxtqscz();
    }




}
